package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.dmom.mes.bean.ProductRoute;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.ProductRouteDataHandler;
import com.efast.dmom.mes.entity.PieceWageEntity;
import com.efast.dmom.mes.service.PieceWageService;
import com.efast.dmom.mes.service.ProductRouteService;

@Transactional
@Service
public class ProductRouteServiceImpl implements ProductRouteService {

	@Autowired
	private ProductRouteDataHandler productRouteDataHandler;

	@Autowired
	private PieceWageService pieceWageService;

	@Override
	public ResultBean save(ProductRoute productRoute) {
		ResultBean resultBean = new ResultBean(true, ' ');
		// check required items
		if (productRoute.getInnerData().getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料是否填写!");
			return resultBean;
		}
		if (productRoute.getInnerData().getRouteId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查工艺路线是否填写!");
			return resultBean;
		}
		ProductRoute productRouteData = productRouteDataHandler.getProductRouteByPartIdAndRouteId(
				productRoute.getInnerData().getPartId(), productRoute.getInnerData().getRouteId());
		if (productRouteData.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("产品工艺关系已存在，请重填!");
			return resultBean;
		}
		List<ProductRoute> productRouteByPartList = productRouteDataHandler
				.getByPartId(productRoute.getInnerData().getPartId());
		if (productRouteByPartList != null && productRouteByPartList.size() > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该产品已绑定其它工艺路线，无法新增！");
			return resultBean;
		}
		productRouteDataHandler.save(productRoute);
		// 生成单件工资维护
		ResultBean res = pieceWageService.savePieceWage(productRoute);
		if (res.isSuccess() && res.getMessage().equals("piece wage save successed")) {
			resultBean.setSuccess(true);
			resultBean.setMessage("保存成功,需维护单价工资!");
		} else if (!res.isSuccess() && res.getMessage().equals("piece wage save failed")){
			resultBean.setSuccess(true);
			resultBean.setMessage("保存失败!");
		} else if (res.isSuccess() && res.getMessage().equals("no piece wage")) {
			resultBean.setSuccess(true);
			resultBean.setMessage("保存成功!");
		}
		return resultBean;
	}

	@Override
	public ResultBean update(ProductRoute productRoute) {
		ResultBean resultBean = new ResultBean(true, ' ');
		boolean flag = false;
		if (productRoute.getInnerData().getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料是否填写!");
			return resultBean;
		}
		if (productRoute.getInnerData().getRouteId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查工艺路线是否填写!");
			return resultBean;
		}
		ProductRoute productRouteDataById = productRouteDataHandler.getById(productRoute.getInnerData().getId());
		if (productRouteDataById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的数据不存在!");
			return resultBean;
		}
		if (productRoute.getInnerData().getPartId() != productRouteDataById.getInnerData().getPartId()) {
			    flag = true;
			if (productRoute.getInnerData().getRouteId() != productRouteDataById.getInnerData().getRouteId()) {
				ProductRoute productRouteData = productRouteDataHandler.getProductRouteByPartIdAndRouteId(
						productRoute.getInnerData().getPartId(), productRoute.getInnerData().getRouteId());
				if (productRouteData.getInnerData() != null) {
					resultBean.setSuccess(false);
					resultBean.setMessage("修改后的产品工艺关系已存在，不可进行修改!");
					return resultBean;
				}
			} else {
				List<ProductRoute> productRouteByPart = productRouteDataHandler
						.getByPartId(productRoute.getInnerData().getPartId());
				if (productRouteByPart != null && productRouteByPart.size() > 0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("修改后的产品已绑定其它工艺路线,无法修改！");
					return resultBean;
				}
			}
		} else {
			if(productRoute.getInnerData().getRouteId() != productRouteDataById.getInnerData().getRouteId()) {
				flag = true;
			}
		}
		productRouteDataHandler.update(productRoute);
		if (flag) {
			pieceWageService.deletePieceWageByPartIdAndRouteId(productRouteDataById);
			ResultBean res2 = pieceWageService.savePieceWage(productRoute);
			if (res2.isSuccess() && res2.getMessage().equals("piece wage save successed")) {
				resultBean.setSuccess(true);
				resultBean.setMessage("修改成功,需重新维护单价工资!");
			} else if (!res2.isSuccess() && res2.getMessage().equals("piece wage save failed")) {
				resultBean.setSuccess(true);
				resultBean.setMessage("修改失败!");
			} else if (res2.isSuccess() && res2.getMessage().equals("no piece wage")) {
				resultBean.setSuccess(true);
				resultBean.setMessage("修改成功!");
			}
			return resultBean;
		}
		resultBean.setSuccess(true);
		resultBean.setMessage("修改成功!");
		return resultBean;
	}

	@Override
	public ResultBean delete(ProductRoute productRoute) {
		ResultBean resultBean = new ResultBean(true, ' ');
		productRouteDataHandler.delete(productRoute);
		pieceWageService.deletePieceWageByPartIdAndRouteId(productRoute);
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功!");
		return resultBean;
	}

	@Override
	public ProductRoute getById(long id) {
		return productRouteDataHandler.getById(id);
	}

	@Override
	public ResultBean verifyItem(ProductRoute productRoute) {
		ResultBean resultBean = new ResultBean(true,' ');
		List<PieceWageEntity> entityList= pieceWageService.getPieceWageListByRouteIdAndPartId(productRoute);
		if (entityList != null && entityList.size() > 0) {
			resultBean.setSuccess(false);
		} else {
			resultBean.setSuccess(true);
		}
		return resultBean;
	}

}
